package com.wits.platform.dao.impl;

import java.util.List;
import java.util.Map;

import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;

import com.wits.platform.common.constants.SysTabConstants;
import com.wits.platform.dao.custom.MenuDaoCustom;
import com.wits.platform.dto.MenuDTO;
import com.wits.platform.entity.Menu;
import com.wits.platform.web.mvc.PageParam;

@Component
public class MenuDaoImpl extends BaseDaoImpl<Menu,String> implements MenuDaoCustom{


	private static String SQL_FIND_MENU_BY_ROLE="select m.* from wits_menu m inner join wits_mmr_role_menu rm on(m.mu_id=rm.mu_id)  where rm.role_id=?";

	
	
	@Override
	public int findChildrenCountByPIds(String ids) {

		String sql=String.format("select count(mu_id) from wits_menu where mu_id_p in(%s)",ids);
		Number num=this.uniqueByNative(sql);
		return num.intValue();
	}

	@Override
	public int deleteInBatch(String ids) {
		String sql=String.format("delete from wits_menu where mu_id in(%s)",ids);
		return this.executeUpdateByNative(sql);
	}

	@Override
	public Page<Menu> findMenuPageByRole(Map<String, Object> searchParams,PageParam pageParam, String roleId) {
		return this.findPageByNative(Menu.class,SQL_FIND_MENU_BY_ROLE, pageParam,roleId);
	}

	@Override
	public List<MenuDTO> findMenuForRole(String roleId) {
		String sql="select m.MU_ID,m.MU_NAME,m.MU_ID_P,case when rm.role_id='"+roleId+"' then 'true' else 'false' end as CHECKED from wits_menu m left join wits_mmr_role_menu rm on(m.mu_id=rm.mu_id) and rm.role_id=?";
		return this.findByHibernateNative(MenuDTO.class, sql, roleId);
	}
	
	public List<MenuDTO> findMenuForRole() {
		String sql="select m.MU_ID,m.MU_NAME,m.MU_ID_P,'false' as CHECKED from wits_menu m";
		return this.findByHibernateNative(MenuDTO.class, sql);
	}
	
	
	
	
	
	@Override
	public List<Menu> findMenuByUserId(String userId) {
		String sql="select distinct m.* from wits_menu m right join wits_mmr_role_menu rm on(rm.mu_id=m.mu_id) inner join wits_mmr_user_role ur" 
					+" on(ur.role_id=rm.role_id) right join wits_role r on(ur.role_id=r.role_id) where r.is_enable=? and ur.user_id=? order by m.mu_ord";
		return this.findByNative(Menu.class, sql,SysTabConstants.SYSTAB_D_TNF_T,userId);
	}

}
